package keye.document_manage.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class FileUtil {

	/**
	 * 判断文件是否存在
	 * @param path 文件路径
	 * @return
	 */
	public static boolean isExistsFiles(String... path){
		if(null ==  path)
			return false;
		for (int i = 0; i < path.length; i++) {
			if(null == path[i])
				return false;
			//TODO 判断文件是否存在，存在性能问题(2016-3-10 11:04:59 说法不准确，一开始连接慢的原因：服务器上的ip地址和数据库中的不一样)
			if(Files.exists(Paths.get(path[i]))){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 复制文件
	 * @param path 源文件路径
	 * @param desPath 目标文件的文件夹路径,必须以File.separator结尾
	 * @return
	 * 	1 复制成功
	 */
	public static int copyFileOne(String path, String desPath) {
		if(null ==  path || null == desPath)
			return -1;
		if(!desPath.endsWith(File.separator))
			return 0;
		try {
			Path srcP = Paths.get(path),desP = Paths.get(desPath);
			if(!Files.exists(desP))
				Files.createDirectories(desP);
			if(Files.exists(srcP)){
				desP = Paths.get(desPath+srcP.getFileName());
				Files.copy(srcP, desP, StandardCopyOption.REPLACE_EXISTING);
			}else{
				return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 1;
	}

	/**
	 * 调用Windows程序打开目录
	 * @param directoryPath 目录路径
	 */
	public static void openDirectory(String directoryPath) {
		String[] list = {"explorer.exe", directoryPath};
		ProcessBuilder pb = new ProcessBuilder(list);
		try {
			pb.start();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * 调用Windows程序打开txt文件
	 * @param logPath 文件路径
	 */
	public static void openTxt(String logPath){
		String[] list = {"notepad.exe", logPath};
		ProcessBuilder pb = new ProcessBuilder(list);
		try {
			pb.start();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		long startTime = System.nanoTime();
		//\\\\192.168.1.178\\28\\2001\\0001\\001\\0001.jpg
//		copyFile("\\\\192.168.1.154\\m/test/ww/", "m:/test/wb5"+File.separator);
//		Path p = Paths.get("\\\\192.168.1.154\\m\\test\\ww\\001.jpg");
		
		long elapsedTime = System.nanoTime() - startTime;
		System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0)
				+ " seconds");		
		
	}
}
